package com.cershy.chatroomserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cershy.chatroomserver.entity.UserList;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserListMapper extends BaseMapper<UserList> {

    // 根据用户ID获取所有加入的群聊
    List<UserList> selectByUserId(String userId);
    @Select("SELECT user_id FROM user_list WHERE group_id = #{groupId} AND role = 'leader' LIMIT 1")
    String selectOwnerIdByGroupId(String groupId);

    @Select("SELECT u.id AS userId, u.name, u.avatar, ul.role " +
            "FROM user_list ul " +
            "JOIN user u ON ul.user_id = u.id " +
            "WHERE ul.group_id = #{groupId}")
    List<Map<String, Object>> selectGroupUsers(String groupId);

    // 判断用户是否属于某个群聊
    UserList selectByUserIdAndGroupId(String userId, String groupId);

    int deleteByUserIdAndGroupId(@Param("userId") String userId, @Param("groupId") String groupId);


    List<String> selectUserIdsByGroupId(String groupId);

}
